﻿function DoAction(grid, callback, message) {
    var selected = grid.getSelectedRow();
    if (selected)
        callback(selected);
    else
        $.ligerDialog.warn(message, '提示');
}

function DoActionConfirm(grid, callback, message, confirm) {
    DoAction(grid, function (selected) {
        $.ligerDialog.confirm(message, '提示', function (res) {
            if (res)
                callback(selected);
        });
    }, message);
}

function formatNullable(item) {
    return item.Nullable ? '是' : '否';
}

(function ($) {    
    if (!liger.options) {
        liger.options = {};
    }

    if (!liger.events)
        liger.events = {};
    if (!liger.dialog) {
        liger.dialog = function (id, row) {
            var options = liger.options[id];
            options.dialog = $.ligerDialog.open({
                target: $('#' + id),
                width: options.width,
                height: options.height,
                title: options.title,
                isResize: true,
                modal: true,
                buttons: [
                    {
                        text: "确定", onclick: function () {
                            var m = liger.events[id + '_click'];
                            if (m) m();
                        }
                    },
                    {
                        text: "取消", onclick: function () {
                            options.dialog.hide();
                        }
                    }
                ]
            });
        }
    }

    function toolbar_click(item) {
        var m = liger.events[item.method];
        if (m) m();
    }

    function initGrid(control, options) {
        var gridOptions = options[control.attr('id')];
        if (gridOptions)
            if (gridOptions.toolbar) {
                $.each(gridOptions.toolbar, function (i, n) {
                    n.click = toolbar_click;
                });
            }

        control.ligerGrid({
            title: gridOptions.title,
            columns: gridOptions.columns,
            toolbar: { items: gridOptions.toolbar },
            usePager: gridOptions.usePager || true,
            rownumbers: true,
            resizable: false,
            frozen: false,
            dataAction: 'local'
        });
    }

    function initForm(control, options) {
        control.ligerForm(options[control.attr('id')]);
    }

    function initDlg(control, options) {
        var id = control.attr('id');
        liger.options[id] = options[id];
    }

    $.fn.initWindow = function (options) {
        $(this).find('[data-common]').each(function (i, e) {
            var control = $(e);
            eval('var data = {' + control.attr('data-common') + '}');
            switch (data.type) {
                case 'grid':
                    initGrid(control, options);
                    break;

                case 'form':
                    initForm(control, options);
                    break;

                case 'dlg':
                    initDlg(control, options);
                    break;
            }
        });
    }
})(jQuery);
